
-- =============================================
-- Author:		Rosario Silva
-- Create date: 02/09/2012
-- Description:	Obtener listado de Area.
-- =============================================
CREATE PROCEDURE [dbo].[Area_R]		
@Name NVARCHAR(50),
@Start INT, 
@Limit INT, 
@Order NVARCHAR(50),
@Direction NVARCHAR(5)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
	SELECT 
	    ROW_NUMBER() OVER ( ORDER BY CASE WHEN @Order = 'Name'AND @Direction = 'ASC' THEN nombre END ASC,	
	                       CASE WHEN @Order = 'Name'AND @Direction = 'DESC' THEN nombre END DESC	
	                      ) AS RowNum,	            
		id,
		nombre
		INTO #Area
		FROM Area
		WHERE (nombre LIKE '%'+@Name+'%' OR @Name = '')
		  AND activo = 1
		SELECT COUNT(id)AS TotalReg From #Area
		SELECT 
			id, 
			nombre
			FROM    #Area	
		WHERE   RowNum BETWEEN @Start + 1 AND (@Start + @Limit)
	DROP TABLE #Area		   
	SET NOCOUNT OFF;
END

GO


